
import 'dart:ui';

import 'package:get/get.dart';
import 'package:share_fit/app/helper/storage_helper.dart';
import 'package:share_fit/app/tools/app_const.dart';
import 'package:share_fit/app/tools/log_utils.dart';
import 'package:share_fit/app/ute/uteped_const.dart';
import 'package:share_fit/app/ute/uteped_device.dart';
import 'package:share_fit/app/ute/uteped_mixin.dart';

class LocaleTool with UTEPedMixin {

  static const lanZhCN = 1;
  static const lanEnUS = 2;

  static const lanCodeZhCN = "zh_CN";
  static const lanCodeEnUS = "en_US";

  static const List languages = [
    {'name': '简体中文', 'language': lanZhCN, 'code': lanCodeZhCN},
    {'name': 'English', 'language': lanEnUS, 'code': lanCodeEnUS}
  ];

  static String locale = lanCodeEnUS;

  static final LocaleTool inst = LocaleTool._();
  LocaleTool._();

  static Future<void> init() async {
    locale = await inst.getSysLocale();
    logger.d("AppTool init $locale");
  }

  Future<String> getSysLocale() async {
    String locale;
    String? currentLocale = await dRemoteReqAsync("currentLocale", {});
    logger.d("locale: $currentLocale");
    if (currentLocale != null && currentLocale.contains("zh")){
      locale = lanCodeZhCN;
    }
    else {
      locale = lanCodeEnUS;
    }
    logger.d("getSysLocale $locale");

    return locale;
  }

  static bool isLanguageSupport(localeCode) {
    if (localeCode == lanCodeEnUS || localeCode == lanCodeZhCN) {
      return true;
    }

    return false;
  }

  static Future<void> updateLocale(localeCode) async {
    logger.d("update locale $localeCode");
    List<String> lanList = localeCode.split('_');

    Locale locale;
    if (isLanguageSupport(localeCode)) {
      locale = Locale(lanList[0], lanList.length == 1 ? null : lanList[1]);
    }
    else {
      logger.d("language $localeCode not support");
      locale = const Locale("en", "US");
    }

    await Get.updateLocale(locale);
  }

  void onLocalChanged(Locale? locale) {
    if (locale == null) {
      return;
    }

    var followSystem = StorageHelper.read(UTEPedConst.keyLanguageFollowSystem, defaultValue: false);

    logger.d("language ${locale.languageCode}");
    var prevLan = StorageHelper.read(UTEPedConst.keyLanguage, defaultValue: 0);
    int currentLan;
    String tempLocale = lanCodeZhCN;
    if (locale.languageCode.contains("zh")) {
      currentLan = lanZhCN;
    }
    else {
      currentLan =  lanEnUS;
      tempLocale = lanCodeEnUS;
    }

    logger.d("prev and current: $prevLan-$currentLan");

    if (LocaleTool.locale != tempLocale) {
      logger.d("language previous ${LocaleTool.locale}");
      StorageHelper.save("language_changed", true);

      if (!followSystem) {
        StorageHelper.save(UTEPedConst.keyLanguage, currentLan);
      }
      else {
        setRemoteLanguage(currentLan);
      }

      LocaleTool.locale = tempLocale;
      logger.d("language changed ${LocaleTool.locale}");
    }
  }


  // 获取配置语言
  static List<String> configLaunch() {
    List<String> list = ["en", 'US'];
    var followSystem = true;
    if (StorageHelper.hasData(UTEPedConst.keyLanguageFollowSystem)) {
      followSystem = StorageHelper.read(UTEPedConst.keyLanguageFollowSystem, defaultValue: false);
    }
    else {
      StorageHelper.save(UTEPedConst.keyLanguageFollowSystem, true);
    }

    String locale;
    if (followSystem) {
      locale = LocaleTool.locale;
    }
    else {
      locale = StorageHelper.read(UTEPedConst.keyLanguageCode, defaultValue: lanCodeEnUS);
    }
    List<String> lanList = locale.split('_');
    if (lanList.length == 2) {
      list = lanList;
    }
    return list;
  }

  Future<void> setRemoteLanguage(language) async {
    var connected = await UTEPedDevice.instance.isConnected();
    if (connected) {
      bRemoteReqAsync(UTEPedConst.methodSetLanguage, {UTEPedConst.keyLanguage: language});
    }
  }

  static int getSysLanguage() {
    return locale == lanCodeZhCN ? lanZhCN : lanEnUS;
  }

  //在线协议header字段使用
  static String getCurrentLanStr() {
    return isCN() ? "cn" : "en";
  }

  static bool isCN() {
    return locale == lanCodeZhCN;
  }

  @override
  String getRemotePath() {
    return UTEPedConst.pathCommon;
  }
}